package com.lh.web.controller.shop;

import com.lh.common.entity.admin.Category;
import com.lh.common.enums.*;
import com.lh.common.utils.AssertUtil;
import com.lh.common.utils.Constant;
import com.lh.common.utils.PageUtils;
import com.lh.common.utils.Result;
import com.lh.web.service.serviceAdmin.shop.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author FH
 * @ClassName: CategoryController
 * @Description: (分类管理)
 * @date: 2020-06-28 9:18
 */
@Api(value="CategoryAction",tags={"分类管理"})
@RestController
@RequestMapping("category")
public class CategoryAction {

    @Autowired
    CategoryService categoryService;

    /**
     * 查询分类管理分页
     */
    @ApiOperation(value = "查询分类管理分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "sortName", value = "类目名称", required = false, dataTypeClass= String.class)
    })
    @GetMapping("/getCategoryList")
//    @RequiresPermissions("category:getCategoryListPage")
    public Result<Category> getCategoryList(String sortName,Integer pageNum, Integer pageSize) {
        pageNum = pageNum== null ? 1:pageNum;
        pageSize = pageSize== null ? Integer.MAX_VALUE:pageSize;
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        params.put("sortName", sortName);
        PageUtils<Category> shopMyPage = categoryService.queryCategoryList(params,pageNum,pageSize);
        return Result.ok(shopMyPage);
    }

    /**
     * 新增分类
     */
    @ApiOperation(value = "新增/修改分类")
    @PostMapping("/setCategory")
//    @RequiresPermissions("category:setCategory")
    public Result<Category> setCategory(@RequestBody Category category) {
        try {
            AssertUtil.assertNotNull(category.getSortName(), Constant.RESULT.FI1000, "类目名称为空!");
            AssertUtil.assertNotNull(category.getSortPic(), Constant.RESULT.FI1000, "类目图片为空!");
            Category categoryName = categoryService.getCategoryName(category.getSortName());
            if(category.getId() == null) {
                if(categoryName != null){
                    return Result.error("类目名称已存在!");
                }
                if (category.getSortParentId() == null) {
                    category.setSortParentId(0l);
                }
                categoryService.addCategory(category);
                return Result.ok();
            }
            AssertUtil.assertNotNull(category.getId(), Constant.RESULT.FI1000, "类目ID为空!");
            categoryService.updateCategory(category);
            return Result.ok();
        } catch (Exception e) {
            return Result.error();
        }
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除分类")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "id", value = "分类ID", required = true)
    })
    @GetMapping("/deleteById")
    @ResponseBody
//    @RequiresPermissions("category:deleteById")
    public Result del(Long id){
        try {
            String msg = categoryService.deleteCategory(id);
            if(msg.equals("")){
                return Result.ok();
            }
            return Result.error(msg);
        } catch (Exception e) {
            return Result.error("删除分类异常!");
        }
    }

    @ApiOperation(value = "banner图上下架/排序")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id", value = "id", required = false, dataType = "Long"),
            @ApiImplicitParam(paramType="query",name = "sortReorder", value = "排序", required = false, dataType = "Long"),
            @ApiImplicitParam(paramType="query",name = "sortStatus", value = "状态 UPPERSHELF:显示; LOWERSHELF:不显示", required = false, dataTypeClass = UpperLowerShelfStatus.class)
    })
    @ResponseBody
    @PostMapping("/updateCategoryStatus")
//    @RequiresPermissions("category:updateCategoryStatus")
    public Result updateCategoryStatus(@RequestBody Category category) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("id",category.getId());
        if(category.getSortReorder() != null) {
            params.put("sortReorder",category.getSortReorder());
        }
        if(category.getSortStatus() != null) {
            params.put("sortStatus",category.getSortStatus());
        }
        categoryService.updateCategory(category);
        return Result.ok();
    }
}
